package com.zzyl.config;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 保存 time 到 Redis
     */
    public void saveTime(Long userId, Long time) {
        String key = "user:time:" + userId;
        // 设置 10 分钟过期
        redisTemplate.opsForValue().set(key, time, 10, TimeUnit.MINUTES);
    }

    /**
     * 从 Redis 获取 time
     */
    public Long getTime(Long userId) {
        String key = "user:time:" + userId;
        return (Long) redisTemplate.opsForValue().get(key);
    }
}